package com.gec.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gec.model.system.SysRole;
import com.gec.model.vo.SysRoleQueryVo;
import com.gec.system.service.SysRoleService;
import com.gec.system.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "角色管理系统")
@RestController
@RequestMapping("/admin/system/sysRole") //隔日请求加上父路径
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;

    //        @GetMapping("/findAllRole")
//    public List<SysRole> findAll(){
//        List<SysRole> list = sysRoleService.list();
//        return list;
//    }
//    @ApiOperation("获取查询全部列表")
//    @GetMapping("/findAllRole")
//    public Result findAll() {
//        List<SysRole> list = sysRoleService.list();
//        return Result.ok(list);
//    }

//    @DeleteMapping("/deleteRole/{id}")
//    public void deleteRole(@PathVariable Long id) {
//        System.out.println("删除id：" + id);
//        boolean b = sysRoleService.removeById(id);
//        System.out.println("删除状态：" + b);
//    }
    @ApiOperation("删除")
    @DeleteMapping("/deleteRole/{id}")
    public Result deleteRole(@PathVariable Long id) {
            boolean b = sysRoleService.removeById(id);
            System.out.println("删除状态：" + b);
            if (b) {
                return Result.ok();
            } else {
                return Result.fail();
            }
        }
    // 分页 + 查询
    @ApiOperation("角色分页查询")
    @GetMapping("/{page}/{limit}")
    public Result findRoleByPageQuery(@PathVariable Long page,
                                      @PathVariable Long limit,
                                      SysRoleQueryVo roleQueryVo) {
        IPage<SysRole> page1 = new Page<>(page, limit);
        page1 = this.sysRoleService.selectPage(page1, roleQueryVo);
        return Result.ok(page1);
    }
    // 添加
    @ApiOperation("添加角色")
    @PostMapping("/addRole")
    public Result addRole(@RequestBody SysRole sysRole)
    {
        boolean isSuccess = this.sysRoleService.save(sysRole);
        if (isSuccess)
        {
            return Result.ok();
        }
        else
        {
            return Result.fail();
        }
    }
    // 根据id 去获取一个role
    @GetMapping("/findRoleById/{id}")
    @ApiOperation("根据id查询")
    public Result findRoleById(@PathVariable Long id)
    {
        SysRole sysRole = this.sysRoleService.getById(id);
        return Result.ok(sysRole);
    }
    // 修改
    @ApiOperation("修改角色")
    @PostMapping("/updateRole")
    public Result updateRole(@RequestBody SysRole sysRole)
    {
        boolean isSuccess = this.sysRoleService.updateById(sysRole);
        if (isSuccess)
        {
            return Result.ok();
        }
        else
        {
            return Result.fail();
        }
    }
    // 批量删除
    @ApiOperation("批量删除")
    @DeleteMapping("/batchRemove")
    public Result batchRemove(@RequestBody List<Long> ids)
    {
        boolean isSuccess = this.sysRoleService.removeByIds(ids);
        if (isSuccess)
        {
            return Result.ok();
        }
        else
        {
            return Result.fail();
        }
    }
}
